package IO流;

import javax.imageio.stream.FileImageOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class File3 {
    public static void main(String[] args) {
        String f = "my.txt";
        //字节流建立文件，并写入内容

        try {
            //覆盖方式，建立文件
          //  FileOutputStream fos = new FileOutputStream(f);
            //追加方式，建立文件，如果文件存在，则文件指针移动到文件尾write写入数据
            FileOutputStream fos=new FileOutputStream(f,true);
            fos.write("hello中文效果\r\n".getBytes());
            fos.flush();
            fos.close();//关闭流
            } catch (IOException e) {
                e.printStackTrace();
            }
        //字符流读取文件(文本文件) try with 会自动关In
       f="F:\\IDEA\\maven-String\\src\\main\\java\\IO流\\Dir.java";
        try(var fis =new FileInputStream(f)){
            byte[] buf= fis.readAllBytes();
            String txt=new String(buf);
            System.out.println(new String(buf));
            Pattern p=Pattern.compile("(.*\\r\\n)",Pattern.MULTILINE);

            Matcher m=p.matcher(txt);
            int num=0;
            while(m.find()){
                System.out.print(String.format("%02d %s",++num,m.group()));
            }
          //  fis.readAllBytes();
         } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
